package com.wshl.cloudservice.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.wshl.cloudservice.R;

import java.util.List;

public class MsgListAdapter extends RecyclerView.Adapter<MsgListAdapter.ViewHolder> {

    private Context context;
    private List<String> listData;


    public MsgListAdapter(Context context, List<String> listData) {
        this.context = context;
        this.listData = listData;
    }

    @NonNull
    @Override
    public MsgListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_msg, parent, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        String msg = listData.get(position);
        if (msg.contains("&")) {
            String[] split = msg.split("&");
            String nickname = split[0] + ":";
            String content = split[1];
            LogUtils.i(nickname + ";" + content);

            String newMsg = nickname + content;
            SpannableStringBuilder style = new SpannableStringBuilder(newMsg);
            style.setSpan(new ForegroundColorSpan(Color.parseColor("#50AFFF")), 0, nickname.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            style.setSpan(new ForegroundColorSpan(Color.WHITE), nickname.length() + 1, newMsg.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.tvmsg.setText(style);

        }


    }


    @Override
    public int getItemCount() {
        return listData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvmsg;

        public ViewHolder(View itemView) {
            super(itemView);

            tvmsg = itemView.findViewById(R.id.tv_item_msg);
        }
    }


}
